﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
////using Chefbook.DataObjects;
using NUnit.Framework;
using Rhino.Mocks;
using Chefbook.DataAccessLayer;
using Chefbook.UserActions;

namespace UnitTests.UserActionsTests
{
    [TestFixture]
    public class CreateCategoryActionFixture
    {
        private MockRepository mocks;
        private IUserAction action;
        private ChefbookEntities db;
        private Category category;

        [SetUp]
        public void Init()
        {
            mocks = new MockRepository();
            db = new ChefbookEntities();
            db.ClearDatabase();
            Assert.AreEqual(0, db.Categories.Count(), "Found categories when expected none");
            action = new CreateCategoryAction(null, "Main Dishes") { DatabaseConnector = db };
        }

        [Test]
        public void DoActionTest()
        {
            
            action.DoAction();
            Assert.AreEqual(1, db.Categories.Count(), "Root Category not created");
            var createdCategory = db.Categories.First();
            Assert.AreEqual("Main Dishes", createdCategory.Name);
            
        }

        [Test]
        public void DoActionNonRootCategoryTest()
        {
            action.DoAction();
            var createdCategory = db.Categories.First();
            Assert.AreEqual("Main Dishes", createdCategory.Name);
            Assert.AreEqual(0, createdCategory.Subcategories.Count);
            var action2 = new CreateCategoryAction(createdCategory, "Meat") { DatabaseConnector = db };
            action2.DoAction();
            Assert.AreEqual(1, createdCategory.Subcategories.Count);
            var createdCategory2 = createdCategory.Subcategories.First();
            Assert.AreEqual("Meat", createdCategory2.Name);
        }

        [Test]
        public void UndoActionTest()
        {
            action.DoAction();
            action.UndoAction();
            Assert.AreEqual(0, db.Categories.Count(), "Found categories when expected none");
        }

        [Test]
        public void UndoActionNonRootCategoryTest()
        {
            action.DoAction();
            var createdCategory = db.Categories.First();

            var action2 = new CreateCategoryAction(createdCategory, "Meat") { DatabaseConnector = db };
            action2.DoAction();
            action2.UndoAction();
            Assert.AreEqual(0, createdCategory.Subcategories.Count);
            Assert.AreEqual(1, db.Categories.Count(), "Root category deleted when only sub should have been");
            createdCategory = db.Categories.First();
            Assert.AreEqual("Main Dishes", createdCategory.Name);

            action.UndoAction();
            Assert.AreEqual(0, db.Categories.Count(), "Found categories when expected none");
            

        }

        [Test]
        public void UndoActionWithoutDoingTest()
        {
            UserActionTestsHelper.UndoActionWithoutDoingTest(action);
        }

        [Test]
        public void UndoActionTwiceAfterDoingTest()
        {
            UserActionTestsHelper.UndoActionTwiceAfterDoingTest(action);
        }

        [Test]
        public void DoActionTwiceTest()
        {
            UserActionTestsHelper.DoActionTwiceTest(action);
        }

        [Test]
        public void ActionNameTest()
        {
            Assert.AreEqual(string.Format("Create new Category"), action.ToString());
        }

        [Test]
        public void ActionNameTest2()
        {
            action.DoAction();
            var createdCategory = db.Categories.First();
            Assert.AreEqual("Main Dishes", createdCategory.Name);
            Assert.AreEqual(0, createdCategory.Subcategories.Count);
            var action2 = new CreateCategoryAction(createdCategory, "Meat") { DatabaseConnector = db };
            Assert.AreEqual(string.Format("Create Subcategory in Category '{0}'", createdCategory), action2.ToString());
        }
    }
}
